/*
	Copyright 2009 Omer Saatcioglu
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.

	$Author$
	$Revision$
	$LastChangedDate$
   
*/

package com.saatcioglu.android.guessthenumber;

import java.util.Date;

import android.content.SharedPreferences;

public class CtrlMaintanence {
	
	static final private int NBUTTON_ENABLED = 12;
	static final private int NBUTTON_STATE = 10;
	
	static final public String PREF_NAME = "GuesstheNumber.pref";
	static final private String ELAPSED_TIME  = "isDisplayElapsedTime";
	static final private String TOTAL_SHOTS  = "isDisplayTotalShots";
	static final private String FLAG_LIST  = "isDisplayFlagList";
	static final private String PLAYER_NAME  = "strNameofPlayer";
	static final private String DIGIT_NUMBER  = "nDigitNumber";
	static final private String MAX_SHOTS  = "nMaxShots";
	
	static final public String GAME_SETTINGS_NAME = "GuesstheNumber.GameSettings";
	static final private String GS_GAMESTARTED = "isGameStarted";
	static final private String GS_EDITORTEXT = "mEditorText";
	static final private String GS_STRINGS = "mStrings_";
	static final private String GS_COUNTER = "intCounter";
	static final private String GS_DIGITSPRESSED = "theDigitsPressed";
	static final private String GS_PREVBUTTONS = "arrintPrevButtons_";
	static final private String GS_ELAPSEDTIME = "lngElapsedTime_";
	static final private String GS_BUTTONENABLED = "arrisButtonEnabled_";
	static final private String GS_BUTTONSTATES = "arrintButtonStates";
	
	private SharedPreferences.Editor editor;
	
    public boolean CheckforUpdate(){
    	
        Date DateLastChecked;
        
        long longLastChecked = SMBGuesstheNumber.settings.getLong("DateLastChecked", -1);
        if (longLastChecked == -1)
        {
            DateLastChecked = new Date();
            editor = SMBGuesstheNumber.settings.edit();
            editor.putLong("DateLastChecked", DateLastChecked.getTime());
            editor.commit();
        }
        else
        {
        	Date DateNow = new Date();
        	DateLastChecked = new Date(longLastChecked);
        	if (DateLastChecked.getYear() < DateNow.getYear()
        		|| (DateLastChecked.getYear() == DateNow.getYear() 
        				&& ((DateLastChecked.getMonth() * 30 + DateLastChecked.getDate()) 
        						<= (DateNow.getMonth()*30 + DateNow.getDate() - 7)
        					)
        				)
        			)
        	{
        		editor = SMBGuesstheNumber.settings.edit();
        		editor.putLong("DateLastChecked", DateNow.getTime());
        		editor.putBoolean("isLaunchedBefore", false);
        		editor.commit();
        		return true;
        	}
        	else if (!DateNow.after(DateLastChecked))
        	{
                DateLastChecked = new Date();
                editor = SMBGuesstheNumber.settings.edit();
                editor.putLong("DateLastChecked", DateLastChecked.getTime());
                editor.commit();
        	}
        }
        return false;
    }
    
    public boolean LaunchintheFirstRun(){
    	
        Boolean isLaunchedBefore;
        
        isLaunchedBefore = SMBGuesstheNumber.settings.getBoolean("isLaunchedBefore", false);
        if(!isLaunchedBefore)
        {
        	isLaunchedBefore = true;
        	editor = SMBGuesstheNumber.settings.edit();
        	editor.putBoolean("isLaunchedBefore", isLaunchedBefore);
        	editor.commit();
        	return true;
        }
        return false;
   }

    public void UpdatePreferences(){
    	
        SMBGuesstheNumber.bElapsedTime = SMBGuesstheNumber.settings.getBoolean(ELAPSED_TIME, true);
        SMBGuesstheNumber.bTotalShots = SMBGuesstheNumber.settings.getBoolean(TOTAL_SHOTS, true);
        SMBGuesstheNumber.bDisplayFlagList = SMBGuesstheNumber.settings.getBoolean(FLAG_LIST, false);
        SMBGuesstheNumber.strPlayerName = SMBGuesstheNumber.settings.getString(PLAYER_NAME, "Human");
        SMBGuesstheNumber.nDigits = Integer.parseInt(SMBGuesstheNumber.settings.getString(DIGIT_NUMBER, "3"));
        SMBGuesstheNumber.nMaxShots = Integer.parseInt(SMBGuesstheNumber.settings.getString(MAX_SHOTS, "0"));
    }
    
    public void setandUpgradePlayerName(String strPlayerName){
    	editor = SMBGuesstheNumber.settings.edit();
    	editor.putString(SMBGuesstheNumber.PLAYER_NAME, strPlayerName);
    	editor.commit();
    	SMBGuesstheNumber.strPlayerName = strPlayerName;
    	
    }

    public void resumetheGame(){
    	int i;
    	
        SMBGuesstheNumber.isGameStarted = SMBGuesstheNumber.gameSettings.getBoolean(GS_GAMESTARTED, false);
        if(SMBGuesstheNumber.isGameStarted)
        {
        	SMBGuesstheNumber.mEditorText = SMBGuesstheNumber.gameSettings.getString(GS_EDITORTEXT, "");
            SMBGuesstheNumber.mStrings.clear();
            for(i = 0; SMBGuesstheNumber.gameSettings.contains(GS_STRINGS + Integer.toString(i)); i++)
            {
            	SMBGuesstheNumber.mStrings.add(SMBGuesstheNumber.gameSettings.getString(GS_STRINGS + Integer.toString(i), ""));
            }
    		SMBGuesstheNumber.intCounter = SMBGuesstheNumber.gameSettings.getInt(GS_COUNTER, 0);
    		SMBGuesstheNumber.theDigitsPressed = SMBGuesstheNumber.gameSettings.getInt(GS_DIGITSPRESSED, 0);
    		for(i = 0; i < SMBGuesstheNumber.nDigits; i++)
    		{
    			SMBGuesstheNumber.arrintPrevButtons[i] = SMBGuesstheNumber.gameSettings.getInt(GS_PREVBUTTONS + Integer.toString(i), 0);
    		}
    		SMBGuesstheNumber.lngElapsedTime = SMBGuesstheNumber.gameSettings.getLong(GS_ELAPSEDTIME, 0);

            for(i = 0; SMBGuesstheNumber.gameSettings.contains(GS_BUTTONENABLED + Integer.toString(i)); i++)
            {
            	SMBGuesstheNumber.arrisButtonEnabled[i] = SMBGuesstheNumber.gameSettings.getBoolean(GS_BUTTONENABLED + Integer.toString(i), true);
            }

            for(i = 0; SMBGuesstheNumber.gameSettings.contains(GS_BUTTONSTATES + Integer.toString(i)); i++)
            {
            	SMBGuesstheNumber.arrintButtonStates[i] = SMBGuesstheNumber.gameSettings.getInt(GS_BUTTONSTATES + Integer.toString(i), 0);
            }
        }
    }

    public void savetheGame(){
    	editor = SMBGuesstheNumber.gameSettings.edit();
    	
    	editor.clear();
    	
    	editor.putBoolean(GS_GAMESTARTED, SMBGuesstheNumber.isGameStarted);
    	if(SMBGuesstheNumber.isGameStarted)
    	{
        	int i;
        	editor.putString(GS_EDITORTEXT, SMBGuesstheNumber.mEditorText);
    		i = 0;
    		for(String tmpString: SMBGuesstheNumber.mStrings)
    		{
    			editor.putString(GS_STRINGS + Integer.toString(i), tmpString);
    			i++;
    		}
    		editor.putInt(GS_COUNTER, SMBGuesstheNumber.intCounter);
    		editor.putInt(GS_DIGITSPRESSED, SMBGuesstheNumber.theDigitsPressed);
    		for(i =0; i < SMBGuesstheNumber.nDigits; i++)
    		{
    			editor.putInt(GS_PREVBUTTONS + Integer.toString(i), SMBGuesstheNumber.arrintPrevButtons[i]);
    		}
    		editor.putLong(GS_ELAPSEDTIME, SMBGuesstheNumber.lngElapsedTime);
    		for(i = 0; i < NBUTTON_ENABLED; i++)
    		{
    			editor.putBoolean(GS_BUTTONENABLED + Integer.toString(i), SMBGuesstheNumber.arrisButtonEnabled[i]);
    		}
    		for(i = 0; i < NBUTTON_STATE; i++)
    		{
    			editor.putInt(GS_BUTTONSTATES + Integer.toString(i), SMBGuesstheNumber.arrintButtonStates[i]);
    		}
    	}
		editor.commit();
    }
}
